#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

#include <time.h>
#include <stdlib.h>
#include <assert.h>
#include "randomizeInPlace.h"

int main (void)
{

	int rand_generator = open ("/dev/random", O_RDONLY);
	if (rand_generator < 0)
		return -1;
	int N = 3;

	unsigned char rand_numbers[N];
	read (rand_generator, rand_numbers, N);

	close (rand_generator);
	for (int i = 0; i < N; i++)
		printf ("%i\n", rand_numbers[i]);


	
	//srand(time(NULL));
	//int r,b = 128;
	//r = (int) ( ((double) b+1) * rand() / (RAND_MAX + 1.0));
	//printf("rand: %i\n", r); 


	int per[16] = {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7};
	randomizeInPlace(per);
	for(int i=0; i<16; i++) 
		   printf("per[%i]: %i\n", i,per[i]);

	return 0;
}



/* Implementation of the algo p.103 "Introduction To Algorithms, 2nd Ed"*/
void randomizeInPlace(int res[]) { 
	srand(time(NULL));
	const int b = 15;
	int tmp, r;
	for(int i = 0; i<16; i++) {
		r = (int) ( ((double) b+1) * rand() / (RAND_MAX + 1.0));
		assert(r<16 && r>= 0);
		tmp = res[i];
		res[i] = res[r];
		res[r] = tmp;
	}
}


